package com.example.nisseapp.connection;

import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import org.json.JSONStringer;

import com.example.nisseapp.model.VoteObject;

import android.util.Log;

public class RestJsonConn implements IConnection
{
		//Remember to change the service IP address when testing and when publishing	
		//private final static String SERVICE_URI = "http://10.0.2.2/RestService/VehicleService.svc";
		private final static String SERVICE_URI = "http://10.34.0.255/Nissen/Service1.svc";
		//private final static String SERVICE_URI = "http://10.0.2.2/nissen/Service1.svc";

		public RestJsonConn() 
		{
			// TODO Auto-generated constructor stub
		}
		
		public void MakeChoice(int choice, int episodeId)
		{
			System.out.println("Start MakeChoice");
			try 
			{
				HttpPost request = new HttpPost(SERVICE_URI + "/MakeChoice");
				request.setHeader("Accept", "application/json");
				request.setHeader("Content-type", "application/json");
				
				// Build JSON string            
				JSONStringer vResult = new JSONStringer()                
					.object()                    
					.key("vResult")                        
					.object()                            
					.key("episodeId").value(episodeId)                            
					.key("choice").value(choice)                                                    
					.endObject()                    
					.endObject();
				
				StringEntity entity = new StringEntity(vResult.toString());             
				request.setEntity(entity);             
				
				// Send request to WCF service            
				DefaultHttpClient httpClient = new DefaultHttpClient();            
				HttpResponse response = httpClient.execute(request);             
				Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode()); 
				System.out.println("Saving : " + response.getStatusLine().getStatusCode());
			} 
			catch (Exception e) 
			{
				System.out.println(e);
			}	
		}
		
		public void SaveRegId(String appRegId)
		{
			System.out.println("Start SaveRegId");
			try 
			{
				HttpPost request = new HttpPost(SERVICE_URI + "/SaveRegId");
				request.setHeader("Accept", "application/json");
				request.setHeader("Content-type", "application/json");
				
				// Build JSON string            
				JSONStringer user = new JSONStringer()                
					.object()                    
					.key("user")                        
					.object()                            
					.key("regId").value(appRegId)                                                
					.endObject();
				
				StringEntity entity = new StringEntity(user.toString());             
				request.setEntity(entity);             
				
				// Send request to WCF service            
				DefaultHttpClient httpClient = new DefaultHttpClient();            
				HttpResponse response = httpClient.execute(request);             
				Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode()); 
				System.out.println("Saving : " + response.getStatusLine().getStatusCode());
			} 
			catch (Exception e) 
			{
				System.out.println(e);
			}	
		}
		
		public VoteObject GetVotingResultForEpidsode(int episodeId) 
		{    
			VoteObject voteObj = new VoteObject();
			try 
			{        
				// COnvert episode id to string
				String epId = Integer.toString(episodeId);
				// Send GET request to <service>/GetVehicle/<plate>        
				DefaultHttpClient httpClient = new DefaultHttpClient();        
				HttpGet request = new HttpGet(SERVICE_URI + "/GetVoteObj/" + epId);   
				
				request.setHeader("Accept", "application/json");        
				request.setHeader("Content-type", "application/json"); 
				
				HttpResponse response = httpClient.execute(request); 
				
				HttpEntity responseEntity = response.getEntity();         
				
				// Read response data into buffer        
				char[] buffer = new char[(int)responseEntity.getContentLength()];        
				InputStream stream = responseEntity.getContent();        
				InputStreamReader reader = new InputStreamReader(stream);        
				reader.read(buffer);        
				stream.close();         
				
				System.out.println("Length: " + buffer.length);
				
				JSONObject votingResult = new JSONObject(new String(buffer));        
					
				
				voteObj.SetOption1(votingResult.getInt("Opt1"));
				voteObj.SetOption2(votingResult.getInt("Opt2")); 
				voteObj.SetOptionText1(votingResult.getString("OptText1"));
				voteObj.SetOptionText2(votingResult.getString("OptText2"));
				
				
			} 
			catch (Exception e)
			{       
				System.out.println("Error in GetVotingResultForEpisodeId!!");
				e.printStackTrace(); 
		    }
			
			return voteObj;
		}
}

